package com.augmentum.oes.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.augmentum.oes.common.AppContext;
import com.augmentum.oes.exception.BaseException;
import com.augmentum.oes.util.PropertyUtil;

public class ThreadFilter implements Filter {

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,
            ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        AppContext appContext = AppContext.getInstance();
        appContext.addData("HTTP_SESSION", request.getSession());
        try {
            ServletContext context = request.getServletContext();
            String url = PropertyUtil.getJDBCInfo("static.resource");
            // Static
            context.setAttribute("surl", url);
            chain.doFilter(request, response);
        } catch (BaseException e) {
            throw e;
        } finally {
            appContext.clear();
        }
    }

    public void init(FilterConfig fConfig) throws ServletException {

    }

}
